
Page({
  data: {
    userId: 'user3',
    showContent: 1, // 默认显示内容1
    showMask: true,
    selectedOptionIndex: 0,//下拉框默认选中 电力
    options: ['选项1', '选项2', '选项3'],  // 下拉框选项列表
    b_amount: 0 ,//购买能源数量
    b_price: 0,//出价
    s_amount: 10 ,//购买能源数量
    s_price: 1,//出价
    orderId: '',
    _transactionId: '001',//_开头的：交易中面板的信息
    _energyType: '电力',
    _energyCount: 100.0,
    _clearingPrice: '1.66',
    _method: '购买',
    showPopup: false,  // 控制弹窗的显示与隐藏
    p_auctionId: "",
    p_amount: "",
    p_status: "",
    p_price: "",
    orderList: [
      {
        auctionId: '006',
        time: '2023-05-09 14:15:00',
        price: 20,
        amount: '50',
        status: '买家'
        // 更多字段...
      },
      {
        auctionId: '006',
        time: '2023-05-09 14:15:00',
        price: 20,
        amount: '50',
        status: '买家'
        // 更多字段...
      },
      {
        auctionId: '006',
        time: '2023-05-09 14:15:00',
        price: 20,
        amount: '50',
        status: '买家'
        // 更多字段...
      },
      
    ], // 历史订单数据
  },
  onLoad(){
    let isLogin = wx.getStorageSync('isLogin')
    console.log('::::')
    console.log(isLogin)
    if(isLogin){
      let user = wx.getStorageSync('user')
      this.setData({
        showMask: false,  //关闭遮盖层
        userId: user
      })
      // 加载历史订单
      this.loadHistoryOrders()
    }
  },
  onShow(){
    this.loadHistoryOrders()

  },
  // 切换功能面板
  switchContent: function(event) {
    var content = event.currentTarget.dataset.content;
    this.setData({
      showContent: content,
    });
  },
  // 能源选中下拉框选中响应 
  selectOption: function(event) {
    this.setData({
      selectedOptionIndex: event.detail.value
    });
  },
  submitBuyReq: function(){
    console.log('提交购买申请,能源数量:',this.data.b_amount,'买价：',this.data.b_price);
    var user = this.data.userId
    var amount = this.data.b_amount
    var price = this.data.b_price
    var that = this
    // 请求fabric上的信息
    wx.request({
      // 192.168.211.130 vm
      // 192.168.31.11 windows
      url: 'http://192.168.211.130:3001/reqBuy?x=0&UserId='+user+'&Amount='+amount+'&Price='+price,
      method: 'GET',
      success: function (res) {
        // 请求成功时的处理逻辑
        // const user = JSON.parse(res.data);
        console.log(res); // 打印返回的数据
       
      },
      fail: function (error) {
        // 请求失败时的处理逻辑
        console.error(error);
      }
    });
  },
  submitSellReq: function(){
    console.log('提交出售申请,能源数量:',this.data.s_amount,'卖价：',this.data.s_price);
    var user = this.data.userId
    var amount = this.data.s_amount
    var price = this.data.s_price
    var that = this
    // 请求fabric上的信息
    wx.request({
      // 192.168.211.130 vm
      // 192.168.31.11 windows
      url: 'http://192.168.211.130:3001/reqSell?x=0&UserId='+user+'&Amount='+amount+'&Price='+price,
      method: 'GET',
      success: function (res) {
        // 请求成功时的处理逻辑
        // const user = JSON.parse(res.data);
        console.log(res); // 打印返回的数据
       
      },
      fail: function (error) {
        // 请求失败时的处理逻辑
        console.error(error);
      }
    });
  },
  bindBuyEnergyCount: function (e) {
    this.setData({
      b_amount: e.detail.value
    })
  },
  bindBuyPrice: function (e) {
    this.setData({
      b_price: e.detail.value
    })
  },
  bindSellEnergyCount: function (e) {
    this.setData({
      s_amount: e.detail.value
    })
  },
  bindSellPrice: function (e) {
    this.setData({
      s_price: e.detail.value
    })
  },
  onSearchInput(event) {
    // 更新数据
    this.setData({
      orderId: '' + event.detail.value, // 更新查询订单的id
    });
  },
  // 搜索订单
  onSearch() {
    // 处理搜索按钮点击事件
    console.log('搜索订单id：',this.data.orderId);
    // 根据订单ID进行搜索逻辑
    if(this.data.orderId == ""){
      console.log("load all")
      this.loadHistoryOrders()
    }else{
      var that = this
      // 请求fabric上的信息
      wx.request({
        // 192.168.211.130 vm
        // 192.168.31.11 windows
        url: 'http://192.168.211.130:3001/getOrder?auctionId='+this.data.orderId,
        method: 'GET',
        success: function (res) {
          let a = JSON.parse(res.data)
          console.log(a.status)
          if(a.sellerId.includes(that.data.userId)){
            a.status = "卖家"
          }else{
            a.status = "买家"
          }
          that.setData({
            p_auctionId: a.auctionId,
            p_amount: a.amount,
            p_status: a.status,
            p_price: a.price,
          })
          //todo 弹窗 显示具体信息
        },
        fail: function (error) {
          // 请求失败时的处理逻辑
          console.error(error);
        }
      });
      this.setData({
        showPopup: true
      });
    }
    // 更新数据
    // this.setData({
    //   orderList: [], // 更新历史订单数据
    // });
  },
  // 加载历史订单列表
  loadHistoryOrders(){
    console.log('加载历史订单');
    var user = this.data.userId
    var that = this
    // 请求fabric上的信息
    wx.request({
      // 192.168.211.130 vm
      // 192.168.31.11 windows
      url: 'http://192.168.211.130:3001/queryOrders?userId='+user,
      method: 'GET',
      success: function (res) {
        // 请求成功时的处理逻辑
        let arr = []
        for(let o of res.data){
          arr.push(o)
        }
        that.setData({
          orderList: arr
        })
        console.log(that.data.orderList)
      },
      fail: function (error) {
        // 请求失败时的处理逻辑
        console.error(error);
      }
    });
    
  },
   // 关闭弹窗
   closePopup: function() {
    this.setData({
      showPopup: false
    });
  },
  // 阻止冒泡，点击内容区域不关闭弹窗
  preventBubbling: function() {
    // do nothing
  }
  
});